package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;
import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PGloadInteractionExpressAd2 extends AdBase {
    private volatile static PGloadInteractionExpressAd2 mInstance;
    TTNativeExpressAd Ad;
    private TTAdNative mTTAdNative;
    static Map<String, AdComplete> mMap= new HashMap<String, AdComplete>();
    /**
     * 单例模式
     *
     * @return
     */
    public static PGloadInteractionExpressAd2 getInstance() {
        if (mInstance == null) {
            synchronized (PGloadInteractionExpressAd2.class) {
                if (mInstance == null) {
                    mInstance = new PGloadInteractionExpressAd2();
                }
            }
        }
        return mInstance;
    }


    @Override
    protected void load() {
        if (getPGid()==null||getPGid().length==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();
    }

    //新建
    public void NewMap(){
        Log.e(TAG,mMap.toString());
        if (mMap.size()==0){
            for (String Id:getPGid()){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        for(String key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                getAds(key);
            }
        }
    }

    private void   getAds(String id){
        Log.e(TAG,"PGloadInteractionExpressAd2加载"+ id);
        mTTAdNative= TTAdManagerHolder.get().createAdNative(activity);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(id)
                .setAdCount(1) //请求广告数量为1到3条
                //.setExpressViewAcceptedSize(300, 450) //期望模板广告view的size,单位dp
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理
        mTTAdNative.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e("AD_onError", "load error : " + code + ", " + msg);
                if (listener!=null){
                    listener.onError(code+msg);
                }
            }
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0) {
                    return;
                }
                Ad = ads.get(0);
                bindAdListener(Ad,activity,id);
                if (Ad != null) {
                    Ad.render();
                } else {
                    RxLogUtils.e("没有添加布局");
                }
            }
        });

    }

    private void bindAdListener(final TTNativeExpressAd ad, final Activity activity,String id) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
            @Override
            public void onAdDismiss() {
                Log.d("onAdDismiss","广告关闭");
                if (listener!=null){
                    listener.onPageDismiss();
                }
            }

            @Override
            public void onAdClicked(View view, int type) {
                Log.d("onAdClicked","广告被点击");
                if (listener!=null){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            listener.onAdClicked();
                        }
                    },200);
                }
            }

            @Override
            public void onAdShow(View view, int type) {
                Log.e("onAdShow","广告展示");
                if (listener!=null){
                    listener.onAdShow();
                }
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                Log.e("onRenderFail", msg + " code:" + code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                Log.e("onRenderSuccess","渲染成功");
                ad.showInteractionExpressAd(activity);

            }
        });
        //  bindDislike(ad, false);
        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
            return;
        }
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
                //TToast.show(InteractionExpressActivity.this, "点击开始下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {

            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                // TToast.show(InteractionExpressActivity.this, "下载暂停，点击继续", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                // TToast.show(InteractionExpressActivity.this, "下载失败，点击重新下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                //TToast.show(InteractionExpressActivity.this, "安装完成，点击图片打开", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                // TToast.show(InteractionExpressActivity.this, "点击安装", Toast.LENGTH_LONG);
            }
        });
    }

    @Override
    protected void start() {

    }

    public class AdComplete{
        private boolean Complete;
        private TTFullScreenVideoAd ad;

        public AdComplete(boolean complete, TTFullScreenVideoAd ad) {
            Complete = complete;
            this.ad = ad;
        }

        public boolean isComplete() {
            return Complete;
        }

        public void setComplete(boolean complete) {
            Complete = complete;
        }

        public TTFullScreenVideoAd getAd() {
            return ad;
        }

        public void setAd(TTFullScreenVideoAd ad) {
            this.ad = ad;
        }
    }

}



